<?php

namespace Model;
use Think\Model;
//为sw_auth数据表创建一个Model模型类
//父类model：Think/library/think/model.class.php
class AuthModel extends Model{

    function saveData($data){
        //1.根据以后$data(name/pid/controller/action) 数据生产新的字段
        $newid = $this->add($data);
        //2.制作auth_path
        if($data['auth_pid'] == 0){
            //-1-顶级权限 === 新纪录主键id
            $path = $newid;
        }else{
            //-2-非顶级权限 根据pid获得父级权限信息，进而获得其‘全路径’;
            $pinfo =$this->find($data['auth_pid']);
            $path = $pinfo['auth_pid']."-".$newid;
        }
//        dump($path);

        //3.制作auth_level
        $level = substr_count($path,"-");

        $sql="update sw_auth set auth_path = '$path', auth_level = '$level' where auth_id = '$newid'";
        return  $this->execute($sql);

    }


}